package com.wrench.elevator.tida.service;

import com.wrench.elevator.tida.constant.Result;
import com.wrench.elevator.tida.dao.DataCollectionDao;
import com.wrench.elevator.tida.domain.DataCollection;
import com.wrench.elevator.tida.domain.Page;
import com.wrench.elevator.tida.exception.WrenchException;
import com.wrench.elevator.tida.util.WrenchUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Component
public class DataCollectionService {
    @Resource
    private DataCollectionDao dao;
    private static final Logger logger = LoggerFactory
            .getLogger(DataCollectionService.class);


    
    public Page getYesterdayRecharge(String building_id,String current_page,String limit) throws WrenchException{
  	  if(WrenchUtil.hasNull(building_id,current_page,limit)){
            logger.error("param is not found , building_id = {} ,current_page = {} , limit = {} ",building_id,current_page,limit);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE,Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }

        if(!WrenchUtil.areInt(current_page,limit)){
            logger.error("==> string is not int type, current_page = {} , limit = {} ",current_page,limit);
            throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_CODE,Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }
        Page page=new Page();
        int int_current_pageg=Integer.valueOf(current_page);
        int int_limit=Integer.valueOf(limit);
        List<Map<String,String>> list=null;
        try {
            list=dao.getYesterdayRecharge(building_id,(int_current_pageg-1)*int_limit,int_limit);
        } catch (Exception e) {
            logger.error("==> list=dao.getYesterdayChengti(building_id,(int_current_pageg-1)*int_limit,int_limit) execute error , error = {} ",e);
            throw new WrenchException(Result.DB_ERROR_CODE,Result.DB_ERROR_MESSAGE);

        }

        int total=-1;
        try {
            total=dao.getYesterdayRechargeCount(building_id);
        } catch (Exception e) {
            logger.error("==> total=dao.getYesterdayChengtiCount(building_id) execute error , error = {} ",e);
            throw new WrenchException(Result.DB_ERROR_CODE,Result.DB_ERROR_MESSAGE);
        }

        page.setCurrent_page(int_current_pageg);
        page.setLimit(int_limit);
        page.setTotal(total);
        page.setList(list);
        return page;
  }
    

    public Page getYesterdayChengti(String building_id,String current_page,String limit) throws WrenchException{
    	  if(WrenchUtil.hasNull(building_id,current_page,limit)){
              logger.error("param is not found , building_id = {} ,current_page = {} , limit = {} ",building_id,current_page,limit);
              throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE,Result.PARAM_IS_NOT_FOUND_MESSAGE);
          }

          if(!WrenchUtil.areInt(current_page,limit)){
              logger.error("==> string is not int type, current_page = {} , limit = {} ",current_page,limit);
              throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_CODE,Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
          }
          Page page=new Page();
          int int_current_pageg=Integer.valueOf(current_page);
          int int_limit=Integer.valueOf(limit);
          List<Map<String,String>> list=null;
          try {
              list=dao.getYesterdayChengti(building_id,(int_current_pageg-1)*int_limit,int_limit);
          } catch (Exception e) {
              logger.error("==> list=dao.getYesterdayChengti(building_id,(int_current_pageg-1)*int_limit,int_limit) execute error , error = {} ",e);
              throw new WrenchException(Result.DB_ERROR_CODE,Result.DB_ERROR_MESSAGE);

          }

          int total=-1;
          try {
              total=dao.getYesterdayChengtiCount(building_id);
          } catch (Exception e) {
              logger.error("==> total=dao.getYesterdayChengtiCount(building_id) execute error , error = {} ",e);
              throw new WrenchException(Result.DB_ERROR_CODE,Result.DB_ERROR_MESSAGE);
          }

          page.setCurrent_page(int_current_pageg);
          page.setLimit(int_limit);
          page.setTotal(total);
          page.setList(list);
          return page;
    }

    public Page getYesterdayNewReg(String building_id,String current_page,String limit) throws WrenchException{
    	  if(WrenchUtil.hasNull(building_id,current_page,limit)){
              logger.error("param is not found , building_id = {} ,current_page = {} , limit = {} ",building_id,current_page,limit);
              throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE,Result.PARAM_IS_NOT_FOUND_MESSAGE);
          }

          if(!WrenchUtil.areInt(current_page,limit)){
              logger.error("==> string is not int type, current_page = {} , limit = {} ",current_page,limit);
              throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_CODE,Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
          }
          Page page=new Page();
          int int_current_pageg=Integer.valueOf(current_page);
          int int_limit=Integer.valueOf(limit);
          List<Map<String,String>> list=null;
          try {
              list=dao.getYesterdayNewReg(building_id,(int_current_pageg-1)*int_limit,int_limit);
          } catch (Exception e) {
              logger.error("==> list=dao.getYesterdayNewReg(building_id,(int_current_pageg-1)*int_limit,int_limit) execute error , error = {} ",e);
              throw new WrenchException(Result.DB_ERROR_CODE,Result.DB_ERROR_MESSAGE);

          }

          int total=-1;
          try {
              total=dao.getYesterdayNewRegCount(building_id);
          } catch (Exception e) {
              logger.error("==> total=dao.getYesterdayNewRegCount(building_id) execute error , error = {} ",e);
              throw new WrenchException(Result.DB_ERROR_CODE,Result.DB_ERROR_MESSAGE);
          }

          page.setCurrent_page(int_current_pageg);
          page.setLimit(int_limit);
          page.setTotal(total);
          page.setList(list);
          return page;
    }
    
    
    public Map<String,String> getSumDetail(String building_id) throws WrenchException {
        if(WrenchUtil.hasNull(building_id)){
            logger.error("==> param is not found , building_id = {} ",building_id);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE,Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }
        Map<String,String> sumDetail=null;
        try {
            sumDetail=dao.getSumDetail(building_id);
        } catch (Exception e) {
            logger.error("==> sumDetail=dao.getSumDetail(building_id) execute error , error = {} ");
            throw new WrenchException(Result.DB_ERROR_CODE,Result.DB_ERROR_MESSAGE);
        }
        return sumDetail;

    }

    public Page getRecentChengtiDetails(String building_id, String current_page, String limit) throws WrenchException {
        if(WrenchUtil.hasNull(building_id,current_page,limit)){
            logger.error("param is not found , building_id = {} ,current_page = {} , limit = {} ",building_id,current_page,limit);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE,Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }

        if(!WrenchUtil.areInt(current_page,limit)){
            logger.error("==> string is not int type, current_page = {} , limit = {} ",current_page,limit);
            throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_CODE,Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }
        Page page=new Page();
        int int_current_pageg=Integer.valueOf(current_page);
        int int_limit=Integer.valueOf(limit);
        List<Map<String,String>> list=null;
        try {
            list=dao.getRecentChengtiDetails(building_id,(int_current_pageg-1)*int_limit,int_limit);
        } catch (Exception e) {
            logger.error("==> list=dao.getRecentChengtiDetails(building_id,(int_current_pageg-1)*int_limit,int_limit) execute error , error = {} ",e);
            throw new WrenchException(Result.DB_ERROR_CODE,Result.DB_ERROR_MESSAGE);

        }

        int total=-1;
        try {
            total=dao.getRecentChengtiDetailsCount(building_id);
        } catch (Exception e) {
            logger.error("==> total=dao.getRecentChengtiDetailsCount(building_id) execute error , error = {} ",e);
            throw new WrenchException(Result.DB_ERROR_CODE,Result.DB_ERROR_MESSAGE);
        }

        page.setCurrent_page(int_current_pageg);
        page.setLimit(int_limit);
        page.setTotal(total);
        page.setList(list);
        return page;


    }

    public List<String> get7DaysChengtiDetails(String building_id) throws WrenchException {
        if(WrenchUtil.hasNull(building_id)){
            logger.error("==> param is not found , building_id = {} ",building_id);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE,Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }
        List<String> list=null;
        try {
            list=dao.get7DaysChengtiDetails(building_id);
        } catch (Exception e) {
            logger.error("==> list=dao.get7DaysChengtiDetails(building_id) execute error , building_id = {} , error = {} ",building_id,e);
            throw new WrenchException(Result.DB_ERROR_CODE,Result.DB_ERROR_MESSAGE);
        }
        return list;
    }

    public DataCollection getDataCollection(String building_id) throws WrenchException {
        if(WrenchUtil.hasNull(building_id)){
            logger.error("==> param is not found , building_id = {} ",building_id);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE,Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }
        DataCollection dc=null;
        try {
            dc=dao.getDataCollection(building_id);
        } catch (Exception e) {
            logger.error("==> dc=dao.getDataCollection(building_id) execute error , building_id = {} , error = {} ",building_id,e);
            throw new WrenchException(Result.DB_ERROR_CODE,Result.DB_ERROR_MESSAGE);
        }
        return dc;
    }


    public List<Map<String,String>> getRegDetails() throws WrenchException {
        List<Map<String,String>> data=null;
        try {
            data=dao.getRegDetails();
        } catch (Exception e) {
           logger.error("==> data=dao.getRegDetails() execute error , error = {} ",e);
            throw new WrenchException(Result.DB_ERROR_CODE,Result.DB_ERROR_MESSAGE);

        }
        return data;
    }
}
